/**
 * Copyright (c) 2019 AsHacker All rights reserved.
 * <p>
 * http://www.ashacker.com
 * <p>
 * 版权所有，侵权必究！
 */

package io.renren.modules.app.controller;

import com.alibaba.fastjson.JSON;
import io.renren.common.utils.R;
import io.renren.modules.app.annotation.Login;
import io.renren.modules.app.form.in.AppGoodsDzscForm;
import io.renren.modules.app.form.out.AppGoodsInfoForm;
import io.renren.modules.app.form.out.AppGoodsInfoListForm;
import io.renren.modules.lucky.service.AppGoodsDzscService;
import io.renren.modules.lucky.service.AppGoodsInfoService;
import io.renren.modules.sys.controller.AbstractController;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * APP商品活动列表
 *
 * @author Chen chenchen@ashacker.com
 */
@RestController
@RequestMapping("/app")
@Api("APP商品活动列表")
public class AppGoodsController extends AbstractController {

    @Autowired
    private AppGoodsInfoService appGoodsInfoService;

    @Autowired
    private AppGoodsDzscService appGoodsDzscService;

    /**
     * 获取商品列表
     */
    @GetMapping("getGoodsListByType")
    @ApiOperation("每周商品列表")
    public R getGoodsListByType(@RequestParam String page_start, @RequestParam String page_size, @RequestParam String goods_flag,
                                @RequestParam String week_type, @RequestParam String user_id) {

        logger.info("getGoodsListByType方法执行开始：传入参数为   " + page_start + "---" + page_size + "---" + goods_flag + "---" + week_type);

        String curPage = page_start == null ? "0" : page_start;
        String pageSize = page_size == null ? "7" : page_size;
        String goodsFlag = "0";
        String weekType = week_type == null ? "0" : week_type;    //0为本周 1为下周 -1为以前
        String userId = user_id == null ? "0" : user_id;

        Map<String, Object> map = new HashMap<>();
        // 商品分页
        List<AppGoodsInfoListForm> list = new ArrayList<>();
        try {
            list = appGoodsInfoService.queryPageForApp(curPage, pageSize, goodsFlag, weekType, userId);
        } catch (Exception e) {
            logger.error("保存信息失败,错误原因:" + e.getMessage(), e);
            e.printStackTrace();
        }
        map.put("list", list);

        logger.info("getGoodsListByType方法执行结束：返回值为   " + JSON.toJSONString(map));
        return R.ok(map);
    }

    /**
     * 获取我的商品
     */
    @GetMapping("getMyGoodsList")
    @ApiOperation("我的礼物")
    public R getMyGoodsList(@RequestParam String page_start, @RequestParam String page_size,
                            @RequestParam String goods_flag, @RequestParam String user_id) {

        logger.info("getMyGoodsList方法执行开始：传入参数为   " + page_start + "---" + page_size + "---" + goods_flag + "---" + user_id);

        String curPage = page_start == null ? "0" : page_start;
        String pageSize = page_size == null ? "7" : page_size;
        String userId = user_id == null ? "0" : user_id;
        String goodsFlag = "0";

        Map<String, Object> map = new HashMap<>();
        // 商品分页
        List<AppGoodsInfoListForm> list = new ArrayList<>();
        try {
            list = appGoodsInfoService.queryPageForMyGoods(curPage, pageSize, goodsFlag, userId);
        } catch (Exception e) {
            logger.error("保存信息失败,错误原因:" + e.getMessage(), e);
            e.printStackTrace();
        }
        map.put("list", list);

        logger.info("getMyGoodsList方法执行结束：返回值为   " + JSON.toJSONString(map));
        return R.ok(map);
    }

    /**
     * 获取商品详情
     */
    @GetMapping("getGoodsInfo")
    @ApiOperation("商品详情")
    public R getGoodsInfo(@RequestParam String id) {

        logger.info("getGoodsInfo方法执行开始：传入参数为   " + id);

        String goodsId = id == null ? "" : id;
        AppGoodsInfoForm goodsInfo = new AppGoodsInfoForm();
        try {
            goodsInfo = appGoodsInfoService.getGoodsInfo(goodsId);
        } catch (Exception e) {
            logger.error("保存信息失败,错误原因:" + e.getMessage(), e);
            e.printStackTrace();
        }
        //添加浏览量
        appGoodsInfoService.addViewCount(goodsInfo.getId());

        Map<String, Object> map = new HashMap<>();
        map.put("goodsInfo", goodsInfo);

        logger.info("getGoodsInfo方法执行结束：返回值为   " + JSON.toJSONString(map));
        return R.ok(map);
    }

    @Login
    @PostMapping("addGoodsDzsc")
    @ApiOperation("添加点赞收藏")
    public R addGoodsDzsc(@RequestBody AppGoodsDzscForm dzscForm, @RequestParam String token) {
        logger.info("addGoodsDzsc方法执行开始：传入参数为   token=" + token + "---user_id=" + dzscForm.getUserId() + "---good_id="
                + dzscForm.getGoodsId() + "---dzsc_type=" + dzscForm.getDzscType());

        String userToken = token == null ? "" : token;
        Map<String, Object> map = new HashMap<>();
        try {
            int num = appGoodsDzscService.addGoodsdzsc(userToken, dzscForm.getUserId() + "", dzscForm.getGoodsId() + "", dzscForm.getDzscType() + "");
            if (num > 0) {
                map.put("msg", "增加成功");
            } else {
                map.put("msg", "增加失败");
            }
            map.put("num", num);
        } catch (Exception e) {
            logger.error("保存信息失败,错误原因:" + e.getMessage(), e);
            e.printStackTrace();
        }

        logger.info("addGoodsDzsc方法执行结束：返回值为   " + JSON.toJSONString(map));
        return R.ok(map);
    }

    @Login
    @PostMapping("removeGoodsDzsc")
    @ApiOperation("移除点赞收藏")
    public R removeGoodsDzsc(@RequestParam String token, @RequestParam String user_id,
                             @RequestParam String good_id, @RequestParam String dzsc_type) {
        logger.info("removeGoodsDzsc方法执行开始：传入参数为   token=" + token + "---user_id=" + user_id + "---good_id="
                + good_id + "---dzsc_type=" + dzsc_type);

        String userToken = token == null ? "" : token;
        String userId = user_id == null ? "" : user_id;
        String goodId = good_id == null ? "" : good_id;

        Map<String, Object> map = new HashMap<>();
        try {
            int num = appGoodsDzscService.removeGoodsdzsc(userToken, userId, goodId);
            if (num > 0) {
                map.put("msg", "移除成功");
            } else {
                map.put("msg", "移除失败");
            }
            map.put("num", num);
        } catch (Exception e) {
            logger.error("保存信息失败,错误原因:" + e.getMessage(), e);
            e.printStackTrace();
        }

        logger.info("removeGoodsDzsc方法执行结束：返回值为   " + JSON.toJSONString(map));
        return R.ok(map);
    }

    @Login
    @GetMapping("getUserGoodsDzscList")
    @ApiOperation("获取用户收藏")
    public R getUserGoodsDzscList(@RequestParam String token, @RequestParam String user_id,
                                  @RequestParam String page_start, @RequestParam String page_size) {

        logger.info("getUserGoodsDzscList方法执行开始：传入参数为   token=" + token + "---user_id=" + user_id + "---" + page_start + "---" + page_size);

        String userToken = token == null ? "" : token;
        String userId = user_id == null ? "" : user_id;

        Integer curPage = page_start == null ? 0 : Integer.valueOf(page_start);
        Integer pageSize = page_size == null ? 7 : Integer.valueOf(page_size);

        Map<String, Object> map = new HashMap<>();
        // 商品分页
        List<AppGoodsInfoListForm> list = new ArrayList<>();
        try {
            list = appGoodsDzscService.queryPageForFavors(userToken, userId, curPage, pageSize);
        } catch (Exception e) {
            logger.error("保存信息失败,错误原因:" + e.getMessage(), e);
            e.printStackTrace();
        }
        map.put("list", list);
        logger.info("getUserGoodsDzscList方法执行结束：返回值为   " + JSON.toJSONString(map));
        return R.ok(map);
    }

}
